﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './tuiguang.scss'
import Hearder from '../index/hearder'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
    	result:[],
      pageNumber:0,
      hasNextPage:false,
      scrollPd:false,
      navindex:1,
      znwns:'',
      status:''
    }
  }
  config = {
    navigationBarTitleText: '全部订单',
  }
  componentWillMount () {
  	var _this = this
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
        _this.getdate(0)
      }
    })
  }
  componentDidMount() {}
  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}
  getdate(pageNumber){
  	var id=this.$router.params.type
  	var status=this.state.status;
  	var _this=this;
  	var url='/retail/papi/v1.9/share-orders/shareOrdersPage?sharerId.equals='+id+'&page='+pageNumber
  	if(status){
  		url=url+'&settlementStatus.in='+status
  	}
  	
  	CommonJs.ajaxRequest(url,'','GET',function(res){
     if(res.data.page<res.data.totalPages){
     		var hasNextPage = true
     	 }
     	 else{
     		var hasNextPage = false
     	 }
     	  var results = _this.state.result
         var result = res.data.list;
         if(pageNumber == 0) {
           results = []
         }
     	  for(var i = 0; i < result.length; i++){
     			results.push(result[i])
     	  }
     	  if (hasNextPage) {
     		_this.setState({
     		  result:results,
          znwns: '暂无订单',
     		  pageNumber: res.data.page,
     		  hasNextPage: hasNextPage,
     		  scrollPd: true
     		})
     	  }
     	  else {
     		_this.setState({
     		  result:results,
          znwns: '暂无订单',
     		  pageNumber: res.data.page,
     		  hasNextPage: hasNextPage,
     		  scrollPd: false
     		})
     	  }

     },'wfw')
  }
  loadMore(){
    var _this = this
    var pd = _this.state.scrollPd
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1
      _this.getUserOrder(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage
      if(hasNextPage){
        Taro.showToast({
          title:'正在加载数据...',
          icon: 'none',
          duration: 2000
        })
      }else{
        Taro.showToast({
          title:'没有更多数据啦！',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
  changestates(status,val){
    this.setState({
      result:[],
      pageNumber:0,
      navindex:val,
      znwns:'',
      status:status
    })
    var _this=this;
    setTimeout(function(){
      _this.getdate(0)
    },1000)
  }
  dostatus(type){
  	if(type=='INITIALIZATION'||type=='WAITSETTLE'){
  		return '未结算'
  	}
  	else if(type=='HASBEENSETTLED'){
  		return '已结算'
  	}
  	else{
  		return '订单失败（REFUND）'
  	}
  }
  render() {
  	const result = this.state.result
    return (
      <View className="page">
       <View className='qun-bgbanner'>
          <Text className={this.state.navindex==1?'title activer':'title'} onClick={this.changestates.bind(this,'',1)}>全部</Text>
          <Text className={this.state.navindex==2?'title activer':'title'} onClick={this.changestates.bind(this,'WAITSETTLE',2)}>待返佣</Text>
          <Text className={this.state.navindex==3?'title activer':'title'} onClick={this.changestates.bind(this,'HASBEENSETTLED',3)}>已到账</Text>
        </View>
        <ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
	        <View className="tuiguang-list" style={result&&result.length>0?'':'display:none'}>
	        	{result.map((item, index) => (
	          <View className="tuiguang-dl">
	            <View className="tuiguang-l1">
	              <Text className="tuiguang-1">{item.shopName}</Text>
	              <Text className="tuiguang-2">下单时间: {CommonJs.FormatDate(item.createAt,3)}</Text>
	            </View>
	            <View className="tuiguang-l2">
	              <View className="tg-l">
	                <Image
	                  className="tg-img"
	                  src={item.orderItemList[0].itemPicture?item.orderItemList[0].itemPicture:COMMONJS.getlogo()}
	                ></Image>
	              </View>
	              <View className="tg-r">
	                <Text className="tg-r-li" style="color:#333">
	                  订单号: {item.orderId}
	                </Text>
	                <Text className="tg-r-li" style="font-size:14px">
	                  付款 {item.amount/100}元
	                </Text>
	
	                <Text className="tg-r-lif">返还 {item.sharerAmount/100}元</Text>
	                <Text className="tg-r-lis">{this.dostatus(item.settlementStatus)}</Text>
	              </View>
	            </View>
	          </View>
	           ))}
	        </View>
	       <View className="tuiguang-list" style={!result||result.length==0?'':'display:none'}>
	       		<View className="nodate">还没有推广收益哟~~</View> 
	       </View>
				</ScrollView>
				
      </View>
    )
  }
}
